/**
 * sensitive-item-report.html page script
 * @author czp
 * date : 2014-04-11
 */
var defDay = 30;
var nowDay = new Date().format("yyyy-MM-dd hh:mm:ss");
var startDay = new Date( new Date() - defDay*24*3600*1000 ).format("yyyy-MM-dd hh:mm:ss");
var timeRange = startDay + " > " + nowDay;
$('input[name="datePicker"]').val(timeRange);
var autoHeight = window.innerHeight - 290;
var asensitiveItem = null;
var items = "#sensitiveItems";
var sensitivityItemSelect = "#sensitivityItemSelect";
refSensitiveItems(timeRange, asensitiveItem);
init();

function refSensitiveItems(timeRange, asensitiveItem) {
    $.ajax({
        url: contextPath + '/report/asensitiveItemCountReport',
        type: 'POST',
        data: {"time_range": timeRange, "asensitiveItem" : asensitiveItem},
        success: function (data) {
            var html_items = "";
            if (data.length !== 0) {
                var i = 1;
                data.forEach(function (m) {
                    html_items +=
                        "<tr>\n" +
                        "  <td class='center'>" + i++ + "</td>\n" +
                        "  <td class='center'>" + m.asensitiveItemName + "</td>\n" +
                        "  <td class='center'>" + m.count + "</td>\n" +
                        "  <td class='center'><div class='blue' id='gritter-center' onclick='getCharts(\""+ m.asensitiveItemName +"\")' href='#'>" +
                        "  <i class = 'icon-zoom-in bigger-130'></i></div></td>\n" +
                        "</tr>";
                })
            } else {
                html_items = "<tr><td colspan='4' class='center'>无数据</td></tr>";
            }
            $(items).html(html_items);
        }
    })
}

function getCharts(itemName) {
    $.ajax({
        url: contextPath + '/report/asensitiveItemCharts',
        type: 'POST',
        data: {"itemName": itemName, "time_range" : timeRange},
        success: function (data) {
            var _data = [];
            var map = [];
            var times = $('input[name="datePicker"]').val();
            var xAxisBuf = printDay(times, ">");
            for (var i = 0; i < data.buckets.length; i++) {
                map.push({
                    "key" : data.buckets[i].key_as_string,
                    "val" : data.buckets[i].doc_count
                })
            }
            for (i = 0; i < xAxisBuf.length; i++) {
                _data[i] = 0;
                map.forEach(function (m) {
                    if (m.key === xAxisBuf[i]) {
                        _data[i] = m.val;
                    }
                })
            }
            $("#barDialog").removeClass('hide').dialog({
                modal : true,
                height: 0.61 * (autoHeight + 200),
                width : (autoHeight + 200),
                buttons : [{
                    text : "关闭",
                    click : function() {
                        $(this).dialog("close");
                    }
                }]
            });
            $("#bar").height(0.61 * (autoHeight));
            var option = {
                title: {
                    text: "【" + itemName + "】保护次数统计"
                },
                color: ['#3398DB'],
                tooltip : {
                    trigger: 'axis',
                    axisPointer : {
                        type : 'shadow'
                    }
                },
                toolbox: {
                    show : true,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                dataZoom: [{
                    type: 'inside'
                }],
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis : [{
                    type : 'category',
                    data : xAxisBuf,
                    axisTick: {
                        alignWithLabel: true
                    }
                }],
                yAxis : [{
                    type : 'value'
                }],
                series : [{
                    name:'保护次数',
                    type:'line',
                    barWidth: '60%',
                    data: _data
                }]
            };
            var myChart = echarts.init(document.getElementById('bar'));
            myChart.showLoading();
            myChart.setOption(option);
            myChart.hideLoading();
        }
    });
}

function init() {
    getSensitivityItems();
}

function getSensitivityItems() {
    $.ajax({
        url : contextPath + '/report/getSensitivityItems',
        type : 'POST',
        success : function(data) {
            $(sensitivityItemSelect).append("<option value=\"" + " " + "\">" + " " + "</option>");
            for (var i = 0; i < data.length; i++) {
                $(sensitivityItemSelect).append("<option value=\"" + data[i] + "\">" + data[i] + "</option>");
            }
        }
    });
}

$("#rest").on('click', function () {
    $('input[name="datePicker"]').val(timeRange);
    $(sensitivityItemSelect).val("");
});

$("#find").on('click', function () {
    var time = $('input[name="datePicker"]').val();
    var sensitivityItem = $(sensitivityItemSelect).val();
    refSensitiveItems(time, sensitivityItem);
});
